
package uestc.bitman.edxapp.courselist;

import com.android.volley.toolbox.ImageLoader.ImageContainer;

import uestc.bitman.edxapp.EdxAppConfigurations;
import uestc.bitman.edxapp.R;
import uestc.bitman.edxapp.cache.ImageCacheManager;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * @category Adapter of course list
 */
public class MyCourseListAdapter extends BaseAdapter{

    // private Context context;

    private LayoutInflater mInflater;

    private List<Course> mCoursesList = null;

    private Drawable mDefaultIamgeDrawable;

    private Drawable mErrorImageDrawable;

    public MyCourseListAdapter(Context context) {
        // this.context = context;
        mInflater = LayoutInflater.from(context);
        mDefaultIamgeDrawable = context.getResources().getDrawable(R.drawable.ic_stub);
        mErrorImageDrawable = context.getResources().getDrawable(R.drawable.ic_empty);
        mCoursesList = new ArrayList<Course>();
    }

    @Override
    public int getCount() {
        return mCoursesList.size();
    }

    @Override
    public Course getItem(int pos) {
        return mCoursesList.get(pos);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.course_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.courseNameView = (TextView) convertView.findViewById(R.id.course_name);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.course_image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (viewHolder.imageRequest != null) {
            viewHolder.imageRequest.cancelRequest();
        }

        try {
            viewHolder.imageRequest = ImageCacheManager.loadImage(EdxAppConfigurations
                    .createRequestUrl(mCoursesList.get(position).getImageUrl()), ImageCacheManager
                    .getImageListener(viewHolder.imageView, mDefaultIamgeDrawable,
                            mErrorImageDrawable));
            viewHolder.courseNameView.setText(mCoursesList.get(position).getDisplayName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    public void reloadCourseList(List<Course> courseList) {
        this.mCoursesList = courseList;
    }

    class ViewHolder {
        
        public TextView courseNameView;

        public ImageView imageView;

        public ImageContainer imageRequest;
    }
}
